import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const Layout = () => import('@/components/Layout/Layout.vue')


export const constantRouterMap = [
  {
    path: '/',
    redirect: '/document',
  },
  {
    path: '/create',
    component: () => import('@/views/form/create')
  },

  {
    id: 'document',
    path: '/document',
    component: Layout,
    meta: { title: '开发指南' },
    children: [{
      id: 'document',
      path: '/document',
      component: () => import('@/views/document/index.vue'),
      meta: { title: '介绍与安装' },
    },]
  },
  {
    id: 'components',
    path: '/components',
    component: Layout,
    meta: { title: '组件' },
    children: [
      {
        id: 'comsDesiger',
        path: '/comsDesiger',
        component: () => import('@/views/coms/comsDesiger.vue'),
        meta: { title: 'FGDesigner 表单编辑器' }
      },
      {
        id: 'comsFormPc',
        path: '/comsFormPc',
        component: () => import('@/views/coms/comsFormPc.vue'),
        meta: { title: 'FGPcForm 表单渲染(PC端)' }
      },

      {
        id: 'comsFormPhone',
        path: '/comsFormPhone',
        component: () => import('@/views/coms/comsFormPhone.vue'),
        meta: { title: 'FGPhoneForm 表单渲染(移动端)' }
      },

      {
        id: 'comsShowPc',
        path: '/comsShowPc',
        component: () => import('@/views/coms/comsShowPc.vue'),
        meta: { title: 'FGPcShow 表单详情(PC端)' }
      },

      {
        id: 'comsShowPhone',
        path: '/comsShowPhone',
        component: () => import('@/views/coms/comsShowPhone.vue'),
        meta: { title: 'FGPhoneShow 表单详情(移动端)' }
      },
    ],
  },
  {
    path: '/config',
    component: Layout,
    meta: { title: '配置属性' },
    children: [
      {
        path: '/config',
        component: () => import('@/views/config/config.vue'),
        meta: { title: '配置属性参数说明' },
      },

    ]
  },
]


export default new Router({
  // mode: 'history', //后端支持可开
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRouterMap.concat()
})

// 兼容路由报错
const originalPush = Router.prototype.push
const originalReplace = Router.prototype.replace
// push
Router.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}
// replace
Router.prototype.replace = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalReplace.call(this, location, onResolve, onReject)
  return originalReplace.call(this, location).catch(err => err)
}